#include <SDL/SDL.h>
#include <Resource.h>
#include <direct.h>
#include <Data.h>
#include <Application/Application.hpp>
#include <MainMenuScene.hpp>
#include <SpaceScene.hpp>
#include <direct.h>
#undef main
int main()
{
    // Vector2 a(0,200);
    // Vector2 b(100,20);
    // Vector2 c(100,20);
    // Log::log("c %f\n",(a*c) * (b*c));
    // Log::log("a %f\n",(b*a) * (c*a));
    // Log::log("b %f\n",(a*b) * (c*b));
    // return 0;
    if (!Application::init())
    {
        SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "错误", "应用初始化失败", nullptr);
        return 1;
    }
    try
    {
        char buff[BUFSIZ];
        getcwd(buff, sizeof(buff));
        Data::getInstance()->setPath(buff);
        Resource::getInstance()->setPath(buff);
        Data::getInstance()->load();
    }
    catch (std::runtime_error &err)
    {
        SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "错误", err.what(), nullptr);
        Data::getInstance()->quit();
        return -1;
    }
    Data *data = Data::getInstance();
    ym::Json::Object appconfig = data->getData("config")->get("application")->getObject();

    int flags = SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE;
    if (appconfig.at("fullscreen")->getBool())
    {
        flags += SDL_WINDOW_FULLSCREEN;
    }
    int width = appconfig.at("width")->getInt();
    int height = appconfig.at("height")->getInt();
    Static::showAABB = data->getData("config")->get("showaabb")->getBool();
    Static::showCollision = data->getData("config")->get("showcollision")->getBool();
    Static::showQuadTree = data->getData("config")->get("showquadtree")->getBool();
    Static::showFacing = data->getData("config")->get("showfacing")->getBool();
    Static::showTopOrder = data->getData("config")->get("showtoporder")->getBool();
    Static::showMesh = data->getData("config")->get("showmesh")->getBool();
    Static::showNavmesh = data->getData("config")->get("shownavmesh")->getBool();
    Static::fps = data->getData("config")->get("fps")->getInt();
    Application app("StarOcean", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, flags);
    app.addScene(new SpaceScene());
    app.setFps(Static::fps);
    app.showFps(true);
    int res = app.exec();
    Application::quit();
    Data::getInstance()->quit();
    return res;
}